package com.free.ppcar;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.free.ppcar.business.ValidateService;
import com.free.ppcar.common.AndroidInterface;
import com.free.ppcar.common.JSObject;
import com.free.ppcar.layout.WebLayout;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.DefaultWebClient;
import com.just.agentweb.PermissionInterceptor;
import com.just.agentweb.WebChromeClient;
import com.just.agentweb.WebViewClient;

public class MainActivity extends AppCompatActivity {

    protected AgentWeb mAgentWeb;
    private CoordinatorLayout mLinearLayout;
    private AlertDialog mAlertDialog;
    private WebLayout iWebLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_web);


        mLinearLayout = (CoordinatorLayout) this.findViewById(R.id.container);
//        if (getSupportActionBar() != null) {
//            // Enable the Up button
//            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        }

        iWebLayout = new WebLayout(this);
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(mLinearLayout, new CoordinatorLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setMainFrameErrorView(R.layout.agentweb_error_page, -1)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .setWebLayout(iWebLayout)
                .setPermissionInterceptor(mPermissionInterceptor)
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)//打开其他应用时，弹窗咨询用户是否前往其他应用
//                .additionalHttpHeader(getUrl(), "Cookie", "3AFB9FE841AD757A6BBBB51C141C82E8F0A1E40BFEBCFB147C1ECD07A67B74A0=6A066E7765E07E1B25B883774B412010965642E2077348592A15328ED23F9A34; 3AFB9FE841AD757A6BBBB51C141C82E8152544724B2B1D32=6CE1C5D03D96D06B; 3AFB9FE841AD757A6BBBB51C141C82E822A8AD551A2C7ACC9D6B3CDA5D8475F6=F3C5A93C62DC0F311A8D265C63BC953E; 3AFB9FE841AD757A6BBBB51C141C82E83D8C08D896D5D76D=78CC0A6C1646DDF6; 3AFB9FE841AD757A6BBBB51C141C82E849C2E6A81C48465B101EACC944477960=8ED7F7A785D91FE6; 3AFB9FE841AD757A6BBBB51C141C82E820259BE64203A0EC80F21DFB92C79477=DF9A329B9DBB89A1; 3AFB9FE841AD757A6BBBB51C141C82E884ADF70C56B1B577526497173777F3B5=DF9A329B9DBB89A1")
                .interceptUnkownUrl() //拦截找不到相关页面的Scheme
                .createAgentWeb()
                .ready()
                .go(getUrl());

        mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterface(mAgentWeb, this));

        //mAgentWeb.getUrlLoader().loadUrl(getUrl());
        iWebLayout.getMailButton().setOnClickListener(v -> {
            Toast.makeText(getApplicationContext(), "兑换停车券ing...", Toast.LENGTH_SHORT).show();
            // 1.领取积分
            mAgentWeb.getJsAccessEntrace().quickCallJs(JSObject.JS_GETCOUPORN);
            try {
                // 休眠
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            // 2.刷新页面
            Toast.makeText(getApplicationContext(), "刷新页面", Toast.LENGTH_SHORT).show();
            mAgentWeb.getJsAccessEntrace().quickCallJs("window.location.href=window.location.href");
            try {
                // 休眠
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            Toast.makeText(getApplicationContext(), "付款", Toast.LENGTH_SHORT).show();
            mAgentWeb.getJsAccessEntrace().quickCallJs(JSObject.JS_PAY);
        });

        iWebLayout.getMailButton().hide();

    }

    protected PermissionInterceptor mPermissionInterceptor = new PermissionInterceptor() {

        @Override
        public boolean intercept(String url, String[] permissions, String action) {
            Log.i(this.getClass().getSimpleName(), "url:" + url + "  permission:" + permissions + " action:" + action);
            return false;
        }
    };

    private com.just.agentweb.WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
//            http://vip.lcmsh.com/
//            http://vip.lcmsh.com/car_park_normal/memberparking
//            http://vip.lcmsh.com/car_park_normal/pay?car_no=%e7%9a%96P59F16
//            http://vip.lcmsh.com/car_park_normal/memberparking
//            Log.i("yyyyyyyyyyyyyyyy", url);
            if (ValidateService.isPayUrl(url)){

//                Log.d("yyyyyyyyyyyyyyyy", "show");
                iWebLayout.getMailButton().show();
            }else {
//                Log.d("yyyyyyyyyyyyyyyy", "hide");
                iWebLayout.getMailButton().hide();
            }
        }
    };
    private com.just.agentweb.WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
//            if (mTitleTextView != null) {
//                mTitleTextView.setText(title);
//            }
        }
    };

    public String getUrl() {
        return "http://vip.lcmsh.com";
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.i("Info", "onResult:" + requestCode + " onResult:" + resultCode);
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //mAgentWeb.destroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }
}